<?php
namespace Home\Controller;
use Think\Controller;
class PersonalController extends Controller {
    public function index()
    {
        $uid = session("uid");
        $upic = M('users')->where(array("uid" => $uid))->select();
        $collections = M('collections')->where(array("c_uid" => $uid))->join('news on news.id=collections.id ')->select();
        $release =M('news')->where(array("n_uid"=>$uid,"satus"=>'1'))->select();
        $request=M('friend')->where(array("my_uid"=>$uid,"fd_satus"=>'-1'))->join('users on users.uid=friend.friend_uid')->select();
        $friend=M('friend')->where(array("my_uid"=>$uid,"fd_satus"=>'1'))->join('users on users.uid=friend.friend_uid')->select();
        $this->collections=$collections;
        for($i = 0; $i < count($friend); $i++){ $friend[$i]["corf"] = $i + 1;}
        for($i = 0; $i < count($request); $i++){ $request[$i]["cord"] = $i + 1;}
        for($i = 0; $i < count($release); $i++){ $release[$i]["col"] = $i + 1;
            if($release[$i]["tid"]== 1){
                $type[$i]='体育';
            }
            elseif($release[$i]["tid"]== 2)
            {
                $type[$i]='时政';

            }
            elseif($release[$i]["tid"]== 3)
            {
                $type[$i]='娱乐';

            }
            elseif($release[$i]["tid"]== 4)
            {
                $type[$i]='科技';

            }

            elseif($release[$i]["tid"]== 5)
            {
                $type[$i]='军事';

            }
            elseif($release[$i]["tid"]== 6)
            {
                $type[$i]='游戏';

            }
            elseif($release[$i]["tid"]== 7)
            {
                $type[$i]='视频';

            }
            $release[$i]["tid"]=$type[$i];
        }
        for ($i = 0; $i < count($collections); $i++){
            $collections[$i]["code"] = $i + 1;
            if($collections[$i]["tid"]== 1){
                $type[$i]='体育';
            }
            elseif($collections[$i]["tid"]== 2)
            {
                $type[$i]='时政';

            }
            elseif($collections[$i]["tid"]== 3)
            {
                $type[$i]='娱乐';

            }
            elseif($collections[$i]["tid"]== 4)
            {
                $type[$i]='科技';

            }

            elseif($collections[$i]["tid"]== 5)
            {
                $type[$i]='军事';

            }
            elseif($collections[$i]["tid"]== 6)
            {
                $type[$i]='游戏';

            }
            elseif($collections[$i]["tid"]== 7)
            {
                $type[$i]='视频';

            }
            $collections[$i]["tid"]=$type[$i];
        }
        if(!$uid)
        {$this->error('登录已过期!',U("Login/index"));}
        else{
            $this->assign('list', $collections);
            $this->assign('request', $request);
            $this->assign('release', $release);
            $this->assign('friend', $friend);
            $this->assign('upic', $upic);
            $this->display();
        }
    }



    public function del(){
        $id = $_GET['id'];
        $collectionsmodel=M('collections');
        $collections=$collectionsmodel->where(array("id"=>$id))->delete();
        if ($collections) {
            $this->success('删除收藏成功');
        } else {
            $this->error('删除收藏失败!');

        }
    }

    public function detail(){
        $uid=session("uid");
        $upic=M('users')->where(array("uid"=>$uid))->select();
        $id = $_GET['id'];
        $news=M('news')->where(array("id" => $id))->join('users on news.n_uid=users.uid ')->select();
        for ($i = 0; $i < count($news); $i++){
            $news[$i]["code"] = $i + 1;
            if($news[$i]["tid"]== 1){
                $type[$i]='体育';
            }
            elseif($news[$i]["tid"]== 2)
            {
                $type[$i]='时政';

            }
            elseif($news[$i]["tid"]== 3)
            {
                $type[$i]='娱乐';

            }
            elseif($news[$i]["tid"]== 4)
            {
                $type[$i]='科技';

            }

            elseif($news[$i]["tid"]== 5)
            {
                $type[$i]='军事';

            }
            elseif($news[$i]["tid"]== 6)
            {
                $type[$i]='游戏';

            }
            elseif($news[$i]["tid"]== 7)
            {
                $type[$i]='视频';

            }
            $news[$i]["tid"]=$type[$i];
        }
        if($news){
            $this->assign('list', $news);
            $this->assign('upic', $upic);
            $this->display();
        }
        else{ $this->error('获取失败!', U("Home/Personal/index"));}
    }


    public function addphoto(){
        $id=session('uid');
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/images/pic/'; // 设置附件上传根目录
        $upload->autoSub = false;
        $info = $upload->uploadOne($_FILES['bgphoto']);
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }
        $data['bgphoto']=$info['savepath'].$info['savename'];
        $a=M("users");
        if($id){ $a->where(array("uid"=>$id))->save($data);
            $this->success('设置成功',U("Home/Personal/index"));}
        else{
            $this->error('设置失败！请重新设置', U("Personal/index"));
        }

    }

    public function delphoto(){
        $uid=session('uid');
        $photo=M('users')->where(array("uid"=>$uid))->select();
        if($photo[0]["bgphoto"]==null){
            $this->error('壁纸已为空', U("Personal/index"));
        }
        else{
            $data['bgphoto'] = null;
            $a=M("users");
            $a->where(array("uid"=>$uid))->save($data);
            $this->success('壁纸已清空',U("Home/Personal/index"));}
        }

    public function search(){
        $text = trim(I('post.text'));
        $newsmodel=M('news');
        $a['title']=array('like',"%{$text}%");
        $select=$newsmodel->where($a)->select();
        for ($i = 0; $i < count($select); $i++){
            $select[$i]["code"] = $i + 1;
            if($select[$i]["tid"]== 1){
                $type[$i]='体育';
            }
            elseif($select[$i]["tid"]== 2)
            {
                $type[$i]='时政';

            }
            elseif($select[$i]["tid"]== 3)
            {
                $type[$i]='娱乐';

            }
            elseif($select[$i]["tid"]== 4)
            {
                $type[$i]='科技';

            }

            elseif($select[$i]["tid"]== 5)
            {
                $type[$i]='军事';

            }
            elseif($select[$i]["tid"]== 6)
            {
                $type[$i]='游戏';

            }
            elseif($select[$i]["tid"]== 7)
            {
                $type[$i]='视频';

            }
            $select[$i]["tid"]=$type[$i];
        }
        if($select)
        {
            $this->assign('list', $select);
            $this->display();
        }
        else{ $this->error('未查到相关结果,请重新查询',U("News/index"));}
    }



    public function adopt()
    {
        $uid=session('uid');
        $id = (int)$_GET['fdid'];
        $d['fd_satus'] = '1';
        $friend = M('friend');
        $yesfriend=M('friend')->where(array("fdid"=>$id,"fd_satus"=>"-1"))->join('users on users.uid=friend.friend_uid')->select();
        $adduser=$yesfriend[0]["username"];
        $already=M('friend')->where(array("fd_satus"=>'1',"my_uid"=>$uid,"username"=>$adduser))->join('users on users.uid=friend.friend_uid')->select();
        $backadd=M('friend')->where(array("fdid"=>$id))->select();
        $data = array('my_uid'=>$backadd[0]["friend_uid"],'friend_uid'=>$backadd[0]["my_uid"],'fd_satus'=>'1');
      if($already){
          $this->error('对方已经是您的好友了，不用添加');
      }
        elseif (!$already){
            $result = $friend->where(array("fdid" => $id))->save($d);
            M("friend")->add($data);
            if ($result) {
                $this->success('成功添加对方好友', U("Personal/index"));
            } else {
                $this->error('通过失败，请通过反馈与管理员进行沟通');
            }
        }


    }



    public function veto(){
        $id = (int)$_GET['fdid'];
        $friend=M('friend')->where(array("fdid"=>$id))->delete();
        if ($friend) {
            $this->success('已拒绝添加对方为好友', U("Personal/index"));
        } else {
            $this->error('拒绝失败，请通过反馈与管理员进行沟通');
        }
    }

    public function frienddel(){
        $id = $_GET['fdid'];
        $delfriend=M('friend')->where(array("fdid"=>$id))->delete();
        if ($delfriend) {
            $this->success('删除该好友成功');
        } else {
            $this->error('删除该好友失败!');
        }
    }

    public function frcomment(){
        $frienduid=session('uid');
        $id = $_GET['fdid'];
        $frcomment= trim(I('post.frcomment'));
        $a=M('friend')->where(array("fdid"=>$id))->select();
        $myuid=$a[0]["friend_uid"];
        $data['friend_uid'] = $frienduid;
        $data['my_uid'] = $myuid;
        $data['frcomment'] = $frcomment;
        $data['frct_fdid'] = $id;
        M('frcomment')->add($data);
        $f=M('frcomment')->where(array("frct_fdid"=>$id))->select();
        if($f){
            $this->success('留言成功');
        }
        else{
            $this->error('留言失败,请重新留言,若再失败,请通过反馈与管理员联系');
        }
    }

    public function fdctindex(){
       $uid=session('uid');
        $a=M('frcomment')->where(array("my_uid"=>$uid))->join('users on users.uid=frcomment.friend_uid')->select();
        for($i = 0; $i < count($a); $i++){ $a[$i]["code"] = $i + 1;}
        if($uid){
            $this->assign('a',$a);
            $this->display();
        }
    }
    public function frcommentdel(){
        $id = $_GET['frctid'];
        $frcommentdel=M('frcomment')->where(array("frctid"=>$id))->delete();
        if ($frcommentdel) {
            $this->success('删除该留言成功');
        } else {
            $this->error('删除该留言失败!');
        }
    }
}